package cn.com.dev;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import cn.com.elite.Util;
import cn.com.kh.KHUtil;
import net.sf.json.JSONObject;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;


public class ApiDemoGetStaff {
    private static final String account = "N00000003082";//替换为您的账户
    private static final String secret = "fd7b96be-b6a1-48b7-a35d-e762008599b7";//替换为您的api密码
    private static final String host = "https://yjwh.tycc100.com/v20160818/sso/getToken/";
    private static Log logger = LogFactory.getLog(ApiDemoGetStaff.class);

    public static void main(String[] args) throws Exception {

        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        String date = sdf.format(new Date());

        String bBase64 = account + ":" + date;
        String Authorization = KHUtil.getbase64(bBase64);

        // 处理头文件
        Map<String, String> headerMap = new HashMap<String, String>();
        headerMap.put("Accept", "application/json");
        headerMap.put("Content-Type", "application/json;charset=utf-8");
        headerMap.put("Authorization", Authorization);

        // sig的值为 32位大写MD5加密 （帐号Id + 帐号APISecret +时间戳）
        String bSign = account + secret + date;
        String sign = KHUtil.getMD5(bSign).toUpperCase();
        System.out.println("sig的值:" + sign);

        String pwd = "tycc100" + account + "8012Paqh@123456" + date;
        System.out.println(pwd);
        String pwds = KHUtil.getMD5(pwd).toLowerCase();
        System.out.println(pwds);

        String postURL = host + account + "?sig=" + sign;
        System.out.println("postURL:" + postURL);

        JSONObject postBody = new JSONObject();
        // postBody.put("account", account);
        postBody.put("exten", "8012");
        postBody.put("password", pwds);
        postBody.put("extentype", "gateway");
        postBody.put("timeStamp", date);
        postBody.put("module", "km");
        System.out.println("发送请求体：" + postBody.toString());

        // 调用API
        String postResult = Util.doHttpPostUseHead(postURL, postBody.toString(), "UTF-8", 120000, 0, headerMap);
        System.out.println("返回请求体：" + postResult);


    }
}
